<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"zhengmingpei.gitee.io","root":"/","images":"/images","scheme":"Pisces","darkmode":false,"version":"8.8.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="C语言小程序在Windows下设置控制台颜色其实很简单，主要通过SetConsoleTextAttribute函数的参数。关于这个函数的原始声明，可以查找MSDN的API说明，也可见下文。改变第二个参数后，再加上简单的循环，可以得到很有趣的效果，就像下图的色表：其实现代码如下: #include &lt;stdio.h&gt;#include &lt;windows.h&gt;int main(v">
<meta property="og:type" content="article">
<meta property="og:title" content="Windows下用C语言设置控制台颜色">
<meta property="og:url" content="https://zhengmingpei.gitee.io/2014/10/22/windowscolor/index.html">
<meta property="og:site_name" content="郑明培的个人博客">
<meta property="og:description" content="C语言小程序在Windows下设置控制台颜色其实很简单，主要通过SetConsoleTextAttribute函数的参数。关于这个函数的原始声明，可以查找MSDN的API说明，也可见下文。改变第二个参数后，再加上简单的循环，可以得到很有趣的效果，就像下图的色表：其实现代码如下: #include &lt;stdio.h&gt;#include &lt;windows.h&gt;int main(v">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://zhengmingpei.gitee.io/2014/10/22/windowscolor/cmdcolor.png">
<meta property="article:published_time" content="2014-10-22T07:00:00.000Z">
<meta property="article:modified_time" content="2021-11-28T03:08:49.713Z">
<meta property="article:author" content="zhengmingpei">
<meta property="article:tag" content="编程">
<meta property="article:tag" content="C语言">
<meta property="article:tag" content="Windows">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://zhengmingpei.gitee.io/2014/10/22/windowscolor/cmdcolor.png">


<link rel="canonical" href="https://zhengmingpei.gitee.io/2014/10/22/windowscolor/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://zhengmingpei.gitee.io/2014/10/22/windowscolor/","path":"2014/10/22/windowscolor/","title":"Windows下用C语言设置控制台颜色"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Windows下用C语言设置控制台颜色 | 郑明培的个人博客</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">郑明培的个人博客</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives" rel="section"><i class="archive fa-fw"></i>归档</a></li>
        <li class="menu-item menu-item-about"><a href="/about" rel="section"><i class="user fa-fw"></i>关于</a></li>
        <li class="menu-item menu-item-categories"><a href="/categories" rel="section"><i class="th fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags" rel="section"><i class="tags fa-fw"></i>标签</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#C%E8%AF%AD%E8%A8%80%E5%B0%8F%E7%A8%8B%E5%BA%8F%E5%9C%A8Windows%E4%B8%8B%E8%AE%BE%E7%BD%AE%E6%8E%A7%E5%88%B6%E5%8F%B0%E9%A2%9C%E8%89%B2"><span class="nav-number">1.</span> <span class="nav-text">C语言小程序在Windows下设置控制台颜色</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#SetConsoleTextAttribute-%E5%87%BD%E6%95%B0%E5%8E%9F%E5%9E%8B"><span class="nav-number">2.</span> <span class="nav-text">SetConsoleTextAttribute()函数原型</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Syntax"><span class="nav-number">2.1.</span> <span class="nav-text">Syntax</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Parameters"><span class="nav-number">2.2.</span> <span class="nav-text">Parameters</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Return-value"><span class="nav-number">2.3.</span> <span class="nav-text">Return value</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Remarks"><span class="nav-number">2.4.</span> <span class="nav-text">Remarks</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Examples"><span class="nav-number">2.5.</span> <span class="nav-text">Examples</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Requirements"><span class="nav-number">2.6.</span> <span class="nav-text">Requirements</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">zhengmingpei</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives">
          <span class="site-state-item-count">36</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories">
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags">
        <span class="site-state-item-count">41</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/zhengmingpei" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;zhengmingpei" rel="noopener" target="_blank"><i class="github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://gitee.com/zhengmingpei" title="gitee → https:&#x2F;&#x2F;gitee.com&#x2F;zhengmingpei" rel="noopener" target="_blank">gitee</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://space.bilibili.com/32918983" title="bilibili → https:&#x2F;&#x2F;space.bilibili.com&#x2F;32918983" rel="noopener" target="_blank">bilibili</a>
      </span>
  </div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://zhengmingpei.gitee.io/2014/10/22/windowscolor/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="zhengmingpei">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="郑明培的个人博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Windows下用C语言设置控制台颜色
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2014-10-22 15:00:00" itemprop="dateCreated datePublished" datetime="2014-10-22T15:00:00+08:00">2014-10-22</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-11-28 11:08:49" itemprop="dateModified" datetime="2021-11-28T11:08:49+08:00">2021-11-28</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%A4%A7%E5%AD%A6/" itemprop="url" rel="index"><span itemprop="name">大学</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="C语言小程序在Windows下设置控制台颜色"><a href="#C语言小程序在Windows下设置控制台颜色" class="headerlink" title="C语言小程序在Windows下设置控制台颜色"></a>C语言小程序在Windows下设置控制台颜色</h2><p>其实很简单，主要通过<code>SetConsoleTextAttribute</code>函数的参数。关于这个函数的原始声明，可以查找MSDN的API说明，也可见下文。<br>改变第二个参数后，再加上简单的循环，可以得到很有趣的效果，就像下图的色表：<br><img src="/2014/10/22/windowscolor/cmdcolor.png" alt="cmdcolor"><br>其实现代码如下:</p>
<figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;windows.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	HANDLE consolehwnd;<span class="comment">//创建句柄，详细句柄知识，请百度一下或查MSDN</span></span><br><span class="line">	consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE);<span class="comment">//实例化句柄</span></span><br><span class="line">	SetConsoleTextAttribute(consolehwnd, <span class="number">15</span>);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;                             Console color list...\n&quot;</span>);</span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">256</span>; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		SetConsoleTextAttribute(consolehwnd, i);</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;%5d&quot;</span>, i);</span><br><span class="line">	&#125;</span><br><span class="line">	SetConsoleTextAttribute(consolehwnd, <span class="number">15</span>);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;hello!\n&quot;</span>);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;It is really beautiful!\n&quot;</span>);</span><br><span class="line">	system(<span class="string">&quot;pause&quot;</span>);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="SetConsoleTextAttribute-函数原型"><a href="#SetConsoleTextAttribute-函数原型" class="headerlink" title="SetConsoleTextAttribute()函数原型"></a>SetConsoleTextAttribute()函数原型</h2><p>SetConsoleTextAttribute function<br>Sets the attributes of characters written to the console screen buffer by the <a target="_blank" rel="noopener" href="http://msdn.microsoft.com/en-us/library/aa365747(v=vs.85).aspx">WriteFile</a> or <a target="_blank" rel="noopener" href="http://msdn.microsoft.com/en-us/library/ms687401(v=vs.85).aspx">WriteConsole</a> function, or echoed by the <a target="_blank" rel="noopener" href="http://msdn.microsoft.com/en-us/library/aa365467(v=vs.85).aspx">ReadFile</a> or <a target="_blank" rel="noopener" href="http://msdn.microsoft.com/en-us/library/ms684958(v=vs.85).aspx">ReadConsole</a> function. This function affects text written after the function call.</p>
<h3 id="Syntax"><a href="#Syntax" class="headerlink" title="Syntax"></a>Syntax</h3><figure class="highlight c"><table><tr><td class="code"><pre><span class="line"><span class="function">BOOL WINAPI <span class="title">SetConsoleTextAttribute</span><span class="params">(</span></span></span><br><span class="line"><span class="params"><span class="function">    _In_  HANDLE hConsoleOutput,</span></span></span><br><span class="line"><span class="params"><span class="function">    _In_  WORD wAttributes</span></span></span><br><span class="line"><span class="params"><span class="function">)</span></span>;</span><br></pre></td></tr></table></figure>

<h3 id="Parameters"><a href="#Parameters" class="headerlink" title="Parameters"></a>Parameters</h3><p>hConsoleOutput [in]</p>
<blockquote>
<p>A handle to the console screen buffer. The handle must have the GENERIC_READ access right. For more information, see Console Buffer Security and Access Rights.</p>
</blockquote>
<p>wAttributes [in]</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="http://msdn.microsoft.com/en-us/library/ms682088(v=vs.85).aspx#_win32_character_attributes">The character attributes</a>.</p>
</blockquote>
<h3 id="Return-value"><a href="#Return-value" class="headerlink" title="Return value"></a>Return value</h3><p>If the function succeeds, the return value is nonzero.<br>If the function fails, the return value is zero. To get extended error information, call <a target="_blank" rel="noopener" href="http://msdn.microsoft.com/en-us/library/ms679360(v=vs.85).aspx">GetLastError</a>.</p>
<h3 id="Remarks"><a href="#Remarks" class="headerlink" title="Remarks"></a>Remarks</h3><p>To determine the current color attributes of a screen buffer, call the <a target="_blank" rel="noopener" href="http://msdn.microsoft.com/en-us/library/ms683171(v=vs.85).aspx">GetConsoleScreenBufferInfo</a> function.</p>
<h3 id="Examples"><a href="#Examples" class="headerlink" title="Examples"></a>Examples</h3><p>For an example, see <a target="_blank" rel="noopener" href="http://msdn.microsoft.com/en-us/library/ms686974(v=vs.85).aspx">Using the High-Level Input and Output Functions</a>.</p>
<h3 id="Requirements"><a href="#Requirements" class="headerlink" title="Requirements"></a>Requirements</h3><p>Minimum supported client<br>Windows 2000 Professional [desktop apps only]<br>Minimum supported server<br>Windows 2000 Server [desktop apps only]</p>
<p>Header</p>
<blockquote>
<p>Wincon.h (include Windows.h)</p>
</blockquote>
<p>Library</p>
<blockquote>
<p>Kernel32.lib</p>
</blockquote>
<p>DLL</p>
<blockquote>
<p>Kernel32.dll</p>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E7%BC%96%E7%A8%8B/" rel="tag"># 编程</a>
              <a href="/tags/C%E8%AF%AD%E8%A8%80/" rel="tag"># C语言</a>
              <a href="/tags/Windows/" rel="tag"># Windows</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2014/10/19/linux2048/" rel="prev" title="Linux控制台版本2048">
                  <i class="fa fa-chevron-left"></i> Linux控制台版本2048
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2014/10/27/windows2048/" rel="next" title="Windows控制台版2048">
                  Windows控制台版2048 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">zhengmingpei</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/pisces/" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
